package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ArrayUtils;
import org.dtrd.modules.evaluation.entity.bean.DiseaseEvaluationInfo;
import org.dtrd.modules.evaluation.entity.dto.DiseaseDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdNephropathy;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdNephropathyMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdNephropathyService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 糖尿病肾病评估 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdNephropathyServiceImpl extends ServiceImpl<DtrdEntEvaluationRdNephropathyMapper, DtrdEntEvaluationRdNephropathy> implements IDtrdEntEvaluationRdNephropathyService {

    @Override
    public DiseaseEvaluationInfo saveNephropathyInfo(DiseaseDTO dto) {
        // 分类或分级诊断
        Map<Integer, String> diagnoseResultMap = new HashMap<>();
        diagnoseResultMap.put(1, "考虑糖尿病肾病可能性大");
        diagnoseResultMap.put(2, "GFR正常");
        diagnoseResultMap.put(3, "慢性肾功能不全（1期）");
        diagnoseResultMap.put(4, "慢性肾功能不全（2期）");
        diagnoseResultMap.put(5, "慢性肾功能不全（3a期）");
        diagnoseResultMap.put(6, "慢性肾功能不全（3b期）");
        diagnoseResultMap.put(7, "慢性肾功能不全（4期）");
        diagnoseResultMap.put(8, "慢性肾功能不全（5期）");
        diagnoseResultMap.put(9, "无糖尿病肾病");
        // 建议复诊时间
        Map<Integer, String>revisitTimeMap = new HashMap<>();
        revisitTimeMap.put(1, "建议及时就诊");
        revisitTimeMap.put(2, "因糖尿病肾病发生率高，建议尽快就诊");
        revisitTimeMap.put(3, "1年复查1次");
        revisitTimeMap.put(4, "至少1年复查1次");
        revisitTimeMap.put(5, "3个月复查1次eGFR");
        revisitTimeMap.put(6, "6个月复查1次eGFR");
        revisitTimeMap.put(7, "每1年复查1次电解质");
        revisitTimeMap.put(8, "每3-6月复查1次电解质、酸碱平衡、甲状旁腺素");
        revisitTimeMap.put(9, "请听取肾内科复诊意见");
        // 就诊科室
        Map<Integer, String> departmentMap = new HashMap<>();
        departmentMap.put(1, "内分泌科就诊");
        departmentMap.put(2, "必要时肾内科就诊");
        departmentMap.put(3, "转诊至肾内科");
        // 具体检查内容推荐
        Map<Integer, String> examinationProjectMap = new HashMap<>();
        examinationProjectMap.put(1, "1、尿常规；<br>" + "2、血肌酐；<br>" + "3、eGFR；<br>" + "4、尿白蛋白排泄；<br>" + "5、血钾");
        // 是否确诊：1-未确诊,2-已确诊,3-确诊无
        int isDiagnose = dto.getIsDiagnose();
        // 症状或检查
        Integer[] symptomArr = dto.getSymptom();
        StringJoiner joiner = new StringJoiner(",");
        for (Integer i : symptomArr) {
            joiner.add(i+"");
        }
        Set<Integer> diagnoseResultSet = new HashSet<>();
        Set<Integer> revisitTimeSet = new HashSet<>();
        Set<Integer> departmentSet = new HashSet<>();
        Set<Integer> examinationProjectSet = new HashSet<>();
        if (1 == isDiagnose) {
            if (ArrayUtils.contains(symptomArr, 1)) {
                diagnoseResultSet.add(1);
                revisitTimeSet.add(1);
                departmentSet.add(1);
                departmentSet.add(2);
                examinationProjectSet.add(1);
            } else if (ArrayUtils.contains(symptomArr, 2)) {
                revisitTimeSet.add(2);
                departmentSet.add(1);
                departmentSet.add(2);
                examinationProjectSet.add(1);
            }
        } else if (2 == isDiagnose) {
            if (ArrayUtils.contains(symptomArr, 1)) {
                diagnoseResultSet.add(2);
                revisitTimeSet.add(3);
                departmentSet.add(1);
                examinationProjectSet.add(1);
            } else if (ArrayUtils.contains(symptomArr, 2)) {
                diagnoseResultSet.add(3);
                revisitTimeSet.add(4);
                departmentSet.add(1);
                examinationProjectSet.add(1);
            } else if (ArrayUtils.contains(symptomArr, 3)) {
                diagnoseResultSet.add(4);
                revisitTimeSet.add(4);
                departmentSet.add(1);
                examinationProjectSet.add(1);
            } else if (ArrayUtils.contains(symptomArr, 4)) {
                diagnoseResultSet.add(5);
                revisitTimeSet.add(5);
                revisitTimeSet.add(7);
                departmentSet.add(1);
                examinationProjectSet.add(1);
            } else if (ArrayUtils.contains(symptomArr, 5)) {
                diagnoseResultSet.add(6);
                revisitTimeSet.add(6);
                revisitTimeSet.add(8);
                departmentSet.add(1);
                examinationProjectSet.add(1);
            } else if (ArrayUtils.contains(symptomArr, 6)) {
                diagnoseResultSet.add(7);
                revisitTimeSet.add(9);
                departmentSet.add(3);
            } else if (ArrayUtils.contains(symptomArr, 7)) {
                diagnoseResultSet.add(8);
                revisitTimeSet.add(9);
                departmentSet.add(3);
            }
        } else if (3 == isDiagnose) {
            diagnoseResultSet.add(8);
            revisitTimeSet.add(3);
            departmentSet.add(1);
            examinationProjectSet.add(1);
        }
        StringBuilder diagnoseResult = new StringBuilder();
        StringBuilder revisitTime = new StringBuilder();
        StringBuilder department = new StringBuilder();
        StringBuilder examinationProject = new StringBuilder();
        for (int diagnoseResultKey : diagnoseResultSet) {
            diagnoseResult.append(diagnoseResultMap.get(diagnoseResultKey)).append("、");
        }
        diagnoseResult = new StringBuilder("".equals(diagnoseResult.toString()) ? "" : diagnoseResult.substring(0, diagnoseResult.length() - 1));
        for (int revisitTimeKey : revisitTimeSet) {
            revisitTime.append(revisitTimeMap.get(revisitTimeKey)).append("，");
        }
        revisitTime = new StringBuilder("".equals(revisitTime.toString()) ? "" : revisitTime.substring(0, revisitTime.length() - 1));
        for (int departmentKey : departmentSet) {
            department.append(departmentMap.get(departmentKey)).append("，");
        }
        department = new StringBuilder("".equals(department.toString()) ? "" : department.substring(0, department.length() - 1));
        // int examinationProjectIndex = 0;
        for (int examinationProjectKey : examinationProjectSet) {
            // examinationProject += (++examinationProjectIndex) + "、" +
            // examinationProjectMap.get(examinationProjectKey) + "\n";
            examinationProject.append(examinationProjectMap.get(examinationProjectKey)).append("<br>");
        }
        examinationProject = new StringBuilder("".equals(examinationProject.toString()) ? "" : examinationProject.substring(0, examinationProject.length() - 4));
        DtrdEntEvaluationRdNephropathy nephropathy = new DtrdEntEvaluationRdNephropathy();
        nephropathy.setCreateTime(new Date());
        nephropathy.setIsDiagnose(isDiagnose);
        nephropathy.setSymptom(joiner.toString());
        nephropathy.setDiagnoseResult(diagnoseResult.toString());
        nephropathy.setRevisitTime(revisitTime.toString());
        nephropathy.setDepartment(department.toString());
        nephropathy.setExaminationProject(examinationProject.toString());
        nephropathy.setUserType(1);
        nephropathy.setOutput(diagnoseResult + "<br>" + revisitTime + "<br>" + department + "<br>" + examinationProject);
        if (nephropathy.getPatientId() != null) {
            nephropathy.setPatientId(dto.getPatientId());
            save(nephropathy);
        }
        return new DiseaseEvaluationInfo().parseFromPo(nephropathy);
    }

    @Override
    public DiseaseEvaluationInfo getNephropathyInfoLatest(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdNephropathy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(DtrdEntEvaluationRdNephropathy::getPatientId, patientId)
                .eq(DtrdEntEvaluationRdNephropathy::getIsDel, 0)
                .orderByDesc(DtrdEntEvaluationRdNephropathy::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdNephropathy nephropathy = baseMapper.selectOne(lambdaQueryWrapper);
        DiseaseEvaluationInfo info = new DiseaseEvaluationInfo();
        if (nephropathy != null) {
            info = info.parseFromPo(nephropathy);
        }
        return info;
    }
}
